In R language you can export string values or dataframes in text and CSV file. For this you can use sink() function to create external connection.

The following method shows how you can do it with syntax.

Method: Use sink() Function

sink("file_name.txt")

sink()

The following example shows how to use this function:

Use sink() Function to Export String to Text File

Let’s see how we can use sink() function to export string to text file:

# Define file name
sink("sample.txt")

# Write text to file
"This is R language"

# Close the connection
sink()

As in the above code we write string data into text file by creating connection using sink() function.

Use sink() Function to Export Data Frame to CSV File

Same like the above you can export data frame to CSV file by creating connection with CSV file:

# Define file name
sink("sample.csv")

# Create data frame
df <- data.frame(Machine_name=c("A","B","C","D","E","F"),
                 Pressure=c(78.2, 71.7, 80.21, 83.12, 82.56, 79.50),
                 Temperature=c(31, 35, 36, 36, 38, 32),
                 Status=c(TRUE,TRUE,FALSE,FALSE,FALSE,TRUE))

# Close the connection
sink()

As in the above code we pass file name to sink() function to create connection then we create dataframe.After this we write dataframe to CSV file and then we close connection.