I’m trying to have two spatial plots side-by-side in shiny, and I was suggested a powerful function, sync of mapview. After figuring how to display mapview object in shiny, I tried to integrate sync in 'shiny', but got the following error: Error in slot(x, "map") : no slot of name "map" for this object of class "shiny.tag.list" . Does it mean sync does not have map object, therefore, it is not possible to integrate sync or latticeView with shiny? If so, I guess there should be workaround solutions and my ears are all open. This is a nice feature to have access from Shiny and allows to do some interesting things. Greatly appreciate any suggestions. Here is the sample reproducible code:
library(shiny)
library(mapview)
ui <- fluidPage(
mapviewOutput("samplemap"),
p()
)
server <- function(input, output, session) {
output$samplemap <- renderMapview({
m1 <- mapview(gadmCHE,zcol="ID_1")
m2 <- mapview(gadmCHE,zcol="OBJECTID")
sync(m1,m2)
})
}
shinyApp(ui, server)
We have discussed making the return value from sync an htmlwidget. Currently, sync returns a htmltools::tagList of the leaflet maps. Inserting tags into shiny will be a little different than inserting mapview. I'll try to explain in code below.
library(mapview)
m1 <- mapview(gadmCHE,zcol="ID_1")
s1 <- sync(m1,m1)
library(shiny)
# if not dynamically adding maps
# we can just insert as ui
shinyApp(
ui = s1,
server = function(input,output){}
)
# if there is a need to create the maps after UI
# then we will need to handle differently
# since sync gives us tags instead of an htmlwidget
shinyApp(
ui = uiOutput("syncmap"),
server = function(input,output){
output$syncmap = renderUI({
s1
})
}
)
Related
I'm trying to plot the outcomes of EDA on to Shiny App, I have been using DataExplorer library for the same and i'm able to perform operations on rmarkdown notebook. I was thinking to integrate the plots to shiny app using the below code but i'm running into errors,Can you please assist me in this regard and also suggest me if there is a way possible to achieve this.
UI part
library(shiny)
library(DataExplorer)
fluidRow(width=12,
column(12,plotOutput("struct"))
)
Server block
df<-read.csv("/path/to/csv/file.csv")
output$struct<-renderPlot({
req(df)
plot_str(df)
})
Thanks for the help in advance
DataExplorer::plot_str by default prints a networkD3::diagonalNetwork, however, it returns a list.
If you want to render the diagonalNetwork object in shiny you'll need to use networkD3::renderDiagonalNetwork. Please check the following:
library(shiny)
library(DataExplorer)
library(datasets)
library(networkD3)
# DF <- read.csv("/path/to/csv/file.csv")
DF <- mtcars
ui <- fluidPage(
fluidRow(column(12, diagonalNetworkOutput("struct")))
)
server <- function(input, output, session) {
output$struct <- renderDiagonalNetwork({
req(DF)
diagonalNetwork(plot_str(DF, print_network = FALSE))
})
}
shinyApp(ui, server)
I have a shiny app (bundled as part of a package) where at the beginning of the server function I create a bunch of reactive data frames that later get used in other parts of the app.
Since quite a few dataframes get created, I wanted to make a simple setup_data() function that could be called at the beginning, in order to help keep the app code tidy. However, since the dfs are created inside a function, I need to use either <<- or assign to make sure they're available in Shiny's server environment.
library(shiny)
setup_data <- function(){
reactiveDat1 <<- shiny::reactiveValues()
reactiveDat1$mydf <<- data.frame(x = 1, y = 2)
reactiveDat2 <<- shiny::reactiveValues()
reactiveDat2$mydf <<- data.frame(x = 5, y = 10)
}
ui <- fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
),
mainPanel(
)
)
)
server <- function(input, output) {
setup_data()
# rest of app goes here....
}
# Run the application
shinyApp(ui = ui, server = server)
Doing it this way generates a cran NOTE of no visible binding for '<<-' assignment, and in general it is bad practise to do global assigns in a cran package.
Therefore, is there way I can create a function that does the setup like this, but in a way conducive to shiny and cran packages? Ideally I'd like to avoid returning everything in a list, and I haven't found a way to make this work in the Shiny Modules framework as there is no corresponding UI to tie these to.
Is there any other options?
You can setup the data using the global.R file. Any R objects that are created in the global.R file become available to the app.R file, or the ui.R and server.R files respectively. Take a look at how to modularize shiny apps.
EDIT: As pointed out in the comments, you can use local = TRUE to only load objects into the shiny environment.
I try to use a function in shiny app.
In ui I only call the function.
ui <- fluidPage(
myfunctionvis<- testforjson(mods, corpus, myDfm)
serVis(myfunctionvis)
)
and in server side exist all the code for this function:
shinyServer(function(input, output) {
testforjson <- function(mods, corpus, myDfm){
# Find required quantities
phi <- posterior(mods)$terms %>% as.matrix
theta <- posterior(mods)$topics %>% as.matrix
# the code continues
return(json_lda)
}
}
and the error I receive is that it can't find the testforjson fuction.
How can I fix the error or include a function in server side and call to see the results in ui?
One way to do that is to create a global.R file in which you can declare variables and functions that can be seen globally in your shiny app.
So you just need to put your testforjson function in global.R.
Check this page for more details: https://shiny.rstudio.com/articles/scoping.html
I want to create a shiny application that has an input for writing some R function or Command, reads it through the ui.R then passes it to the server.R that executes that R command to display the results.
I spent hours searching about some example but couldn't find anything, I already know how to create Shiny apps using ui and server and pass the input values to server and work with them, but I have no idea if it's possible to create a shiny app like R where you can write the commands and return the results, any example or help would be appreciated.
Letting users run code in your app is bad practice, since it comes with great security risks. However, for development you might want to check this function from the shinyjs package by Dean Attali.
Example from the link:
library(shiny)
library(shinyjs)
shinyApp(
ui = fluidPage(
useShinyjs(), # Set up shinyjs
runcodeUI(code = "shinyjs::alert('Hello!')")
),
server = function(input, output) {
runcodeServer()
}
)
Some examples of why it is not such a good idea to include when deploying your app:
Try the input:
shinyjs::alert(ls(globalenv()))
or
shinyjs::alert(list.files())
I was able to find an alternative solution that doesn't require shinyjs -- wanted to restate Florian's concern that in general it is not a good thing (not secure) to let users run code in your Shiny app. Here is the alternative:
library(shiny)
library(dplyr)
ui <- fluidPage(
mainPanel(
h3("Data (mtcars): "), verbatimTextOutput("displayData"),
textInput("testcode", "Try filtering the dataset in different ways: ",
"mtcars %>% filter(cyl>6)", width="600px"),
h3("Results: "), verbatimTextOutput("codeResults"))
)
server <- function(input, output) {
shinyEnv <- environment()
output$displayData <- renderPrint({ head(mtcars) }) # prepare head(mtcars) for display on the UI
# create codeInput variable to capture what the user entered; store results to codeResults
codeInput <- reactive({ input$testcode })
output$codeResults <- renderPrint({
eval(parse(text=codeInput()), envir=shinyEnv)
})
}
shinyApp(ui, server)
I want to display a dataframe in a table using grid.table(myDataFrame). I need help figuring out:
what output* and render* functions to use with shiny
what to write exactly in the render* function body
This far I have the following codes
In the UI.R, inside fluidPage and fluidRow:
dataTableOutput("TauxInsertion")
And then in Server.R:
output$TauxInsertion <- renderDataTable({
dataDepartement()
# TauxInsertionTable <- grid.table(dataDepartement())
# TauxInsertionTable
})
dataDepartement is a reactive variable that contains a dataFrame. Returning this data frame inside the renderDataTable gives me a table. But I need to be able to display the row names and add some color and style to the display. The commented part is what I have tried but doesn't display anything.
There are no significant messages in the console. I have also tried options(shiny.trace=TRUE) but to no avail.
I think you have to use functions dedicated to plot. Take a look
library(shiny)
library(grid)
library(gridExtra)
ui <- fluidPage(
plotOutput("plot")
)
server <- function(input, output) {
output$plot <- renderPlot({
grid.table(head(iris,3))
})
}
shinyApp(ui = ui, server = server)