Hello there!
How can I implement write_clip
or read_clip
, from the pakage clipr
in shinyapps.io? In my computer works perfect, but he server of shiny crashes when these functions are executed.
Here is a minimal example:
Sys.setenv(CLIPR_ALLOW = T)
library(shiny)
library(clipr)
ui <- fluidPage(
sliderInput("obs", "Number of observations", 0, 1000, 500),
actionButton("goButton", "Go!", class = "btn-success"),
plotOutput("distPlot")
)
server <- function(input, output) {
output$distPlot <- renderPlot({
# Take a dependency on input$goButton. This will run once initially,
# because the value changes from NULL to 0.
input$goButton
observeEvent(input$goButton,{
clipr::write_clip("holaaaaaaaaaa")
})
# Use isolate() to avoid dependency on input$obs
dist <- isolate(rnorm(input$obs))
hist(dist)
})
}
shinyApp(ui, server)
The log of the shiny apps return this:
Warning: Error in : Clipboard on X11 requires 'xclip' (recommended) or 'xsel'; Clipboard on Wayland requires 'wl-copy' and 'wl-paste'.
Using clipr with Shiny
clipr won’t do what you expect when you call it with Shiny.
clipr talks to the clipboard of the system that is running R. If you create a Shiny app and tell one of its functions to either read from or write to the clipboard, it can only access the clipboard of the server it is running on. R running on the remote server has no way to access the local clipboard belonging to your end user.
However, you can instruct the user’s internet browser to write to the user’s clipboard by using rclipboard CRAN - Package rclipboard
Oh, thanks, nirgrahamuk, it really worked.
But, if I need to copy a dataframe, what is the key that I am missing? With this method, i need to transform a dataframe into a single string and that string have to be formated to allow the user o paste it in excell.
rclipButton("copy", label= "Copy", clipText = iris)
Ues, format it. I think excel would expect tab delimiters
Oh, nirgrahamuk, I really appreciate your intelligence, and time. In addition to having been fast and accurate.
For future readers: one way to copy a dataframe or a table from a shiny app (alternatives to rclip
) and that works on a server (such as shiny.io
) is to use rclipboard
. Here is a minimal example of how it is must be used. The copied data is actually a long string of characters that excell can interpret as a table when it is pasted into it.
library(shiny)
library(rclipboard)
ui <- fluidPage(
rclipboardSetup(),
sliderInput("obs", "Number of observations", 0, 1000, 500),
uiOutput("goButton_ui"),
plotOutput("distPlot")
)
server <- function(input, output) {
output$distPlot <- renderPlot({
input$goButton
dist <- isolate(rnorm(input$obs))
hist(dist)
})
output$goButton_ui <- renderUI({
print(iris)
rclipButton("goButton", label= "Go!", clipText = format_csv(iris))
})
}
shinyApp(ui, server)