I just wanted to make the data (displayed in the shiny datatable) selected by user editable and eventually to save the user's updates to a file (e.g. such as an excel file), see the testing code below:
I checked the example/questions below(and a few others), but none of those used the reactive as the data source.
https://yihui.shinyapps.io/DT-edit/
Edit datatable in shiny with dropdown selection (for DT v0.19)
DT: Dynamically change column values based on selectinput from another column in R shiny app
The issue for the code below is: when I try to update the second cell, the updates I made in the first cell disappeared.
library(shiny)
library(DT)
#>
#> Attaching package: 'DT'
#> The following objects are masked from 'package:shiny':
#>
#> dataTableOutput, renderDataTable
ui <- fluidPage(
selectInput("data", "Select a dataset:", choices = c("iris", "mtcars")),
DTOutput("t1"),
actionButton("save", "Save")
)
server <- function(input, output, session) {
tempdf <- NULL
data <- reactive({
head(get(input$data))
})
output$t1 <- renderDT({
data()
}, editable = "cell")
proxyt1 <- dataTableProxy("t1")
observeEvent(input$t1_cell_edit, {
info <- input$t1_cell_edit
str(info)
tempdf <<- editData(data(), info)
# tempdf <<- editData(isolate(data()), info)
replaceData(proxyt1, tempdf, resetPaging = FALSE)
})
# observeEvent(input$save, {
# writexl::write_xlsx(tempdf, "~/test.xlsx")
# })
}
shinyApp(ui, server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Shiny applications not supported in static R Markdown documents
Created on 2022-05-23 by the reprex package (v2.0.1)
Thank you very much for your help.
Related
Is there a way to align the row names of a Rhandsontable in a Shiny app? I only found an argument to adjust the width (rowHeaderWidth), but not the alignment of the row names separately from the body of the table that I know can be done with hot_col(). If it's not possible using the rhandsontable() function, what should the CSS code be?
Simple Shiny example:
library(shiny)
library(rhandsontable)
ui <- fluidPage(
rHandsontableOutput('table')
)
server <- function(input,output,session)({
output$table <- renderRHandsontable({
rhandsontable(mtcars, rowHeaderWidth = 200)
})
})
shinyApp(ui = ui, server = server)
If you see screenshot 01 from browser inspection that you need to change text-align.
you can create style.css file and link the file in app (see screenshot 02)
I am trying to use the solution proposed here.
The difference is that I want the user to chose two folders, one to save the result of my code, and one that has all files.
Now the question is that how can I save files in the second folder (input$fileout). Is there a way to pass this path to a function that processes my input files? I think there will be a permission issue.
Edit: I'd like to use dataHandler inside my.analysis() function as I need to save multiple pngs, csv and some specific formatted files. What would be the best approach? My shinyApp does not print out anything, except that analysis is done, download the result.
Here's the updated ui.R
library(shiny)
library(shinythemes)
library(shinyWidgets)
shinyUI(tagList(fluidPage(theme = shinytheme("lumen"),
includeScript("./www/text.js"),
titlePanel("Test"),
fluidRow(
column(4,
tags$div(class="form-group shiny-
input-container",
tags$div(tags$label("Choose a
folder", class="btn btn-primary",
tags$input(id = "fileIn", webkitdirectory = TRUE, type = "file", style="display: none;", onchange="pressed()"))),
tags$label("No folder choosen", id = "noFile"),
tags$div(id="fileIn_progress", class="progress progress-striped active shiny-file-input-progress",
tags$div(class="progress-bar")
)
)),
verbatimTextOutput("tbl")
),
fluidRow(
column(8,
verbatimTextOutput("results"))
)
),
HTML("<script type='text/javascript' src='getFolders.js'>
</script>")
)
)
And my server.R
library(shiny)
library(ggplot2)
library(shinythemes)
library(shinyWidgets)
library(flowCore)
source("myanalysis.R")
options(shiny.maxRequestSize=50*1024^2)
shinyServer(function(input, output, session) {
observeEvent(input$go, {
df <- reactive({
print(input$fileIn)
# Below returns NULL, as it does not have any files in it yet.
print(input$fileout)
inFiles <- input$fileIn
if (is.null(inFiles))
return(NULL)
my.analysis(fs = tmp,output=input$fileout)
})
output$tbl <-renderPrint(
df()
)
output$results = renderPrint({input$mydata
})
})
})
Any help will be greatly appreciated.
Here are two options:
Option 1
It you only want to choose a folder in the sever side (that could be the local system if your app is running locally), you can use ShinyFiles.
Option 2
If your app is running in a server, then is not possible that you can write data to a local directory from your Shiny app. The solution in this case is to save the results in the server and later allow the user to download it using the downloadHandler function.
Greeting Everyone,
I am newbie to ML...i have been learning to built model based on Keras in R and integrating with Shiny app. My test project that i am trying to built is to classify images of forms that receive for processing in my organization. CNN model I have built works fine when loading model in R Script for Prediction. However when try to load the model in Shiny app, i am getting an error
Error: argument "filepath" is missing, with no default
Below is the code of shiny app that i am trying built. Please help with understanding what i am doing incorrectly.
Note: I have tried to call model from within server function / outside of the as well. Both produce the same error.
library(shiny)
library(keras)
library(EBImage)
library(reticulate)
#pmodel <- load_model_weights_hdf5("E:\Sriram\shiny\image_classifier.h5")
ui <- fluidPage("Hello World",
fileInput(inputId = "imgfile",label = "Upload Your Image file",accept = c(".jpg",".jpeg")),
imageOutput("Page"),
"Printing Results",
textOutput("result")
)
server <- function(input,output)
{
output$Page <- renderImage({
src <- input$imgfile
src <- src$datapath
list(src = src,
height = "300",
alt = "This is alternate text")
})
output$result <-renderText({
pmodel <- load_model_weights_hdf5("E:\Sriram\shiny\image_classifier.h5")
img<-readImage(input$imgfile)
rimg <- img
rimg<-resize(rimg,100,100)
x <- array_reshape(rimg,c(1,100,100,3))
prob <- pmodel %>%
predict_proba(x)
pred <- pmodel%>%
predict_classes(x)
c<-c('HCFA','UB','Dental','Superbill-HCFA','Superbill-UB','Medicare', 'COB', 'Attach','Blank','EOB','MEOB')
paste("Image uploaded is -> %s Page with accuary of %s %%",c[pred])
})
}
shinyApp(ui = ui, server = server)`library(shiny)
I was able to find solution for error. Instead of loading weights, I used load_model_hdf5 function for calling in shiny.
I am building an app that presents a data table and which allows you to add data. The adding of data is build by means of a form. This form is written by a module. What I want to happen is that one may fill out the form, press an 'add' button and that the data inside the table is updated.
As an example, could you help me figure out how to complete the following piece of code:
library(shiny)
library(shinydashboard)
moduleInput <- function(id){
ns <- NS(id)
sidebarPanel(
actionButton(ns("action1"), label = "click")
)
}
module <- function(input, output, session){
observeEvent(input$action1, {
# Do stuff here,
# -> let the parent module or server know that something has happened
})
}
ui <- fluidPage(
verbatimTextOutput("module.pressed"),
moduleInput("first")
)
server <- function(input, output, session){
# print the currently open tab
output$module.pressed <- renderPrint({
#-> Write that we have pressed the button of the module
})
callModule(module,"first")
}
shinyApp(ui = ui, server = server)
All I thus want to do is find an easy way to present TRUE in the output field module.pressed when something happend inside the module.
Thanks!
Modules can pass reactive expression(s) to calling apps/modules by returning them in their server function. The documentation provides a few examples on how to set up interactions between modules and calling apps - https://shiny.rstudio.com/articles/modules.html
If a module needs to use a reactive expression, take the reactive expression as a function parameter. If a module wants to return reactive expressions to the calling app, then return a list of reactive expressions from the function.
library(shiny)
moduleInput <- function(id){
ns <- NS(id)
sidebarPanel(
actionButton(ns("action1"), label = "click")
)
}
module <- function(input, output, session){
action1 <- reactive(input$action1)
return(reactive(input$action1))
}
ui <- fluidPage(
verbatimTextOutput("module.pressed"),
moduleInput("first")
)
server <- function(input, output, session){
action1 <- callModule(module,"first")
output$module.pressed <- renderPrint({
print(action1())
})
}
shinyApp(ui = ui, server = server)
I'am using the R package plotly in my shiny application.
Here is a small example:
library(shiny)
library(plotly)
ui <- fluidPage(
plotlyOutput("plot"),
verbatimTextOutput("event")
)
server <- function(input, output) {
# renderPlotly() also understands ggplot2 objects!
output$plot <- renderPlotly({
plot_ly(mtcars, x = ~mpg, y = ~wt)
})
output$event <- renderPrint({
d <- event_data("plotly_hover")
if (is.null(d)) "Hover on a point!" else d
})
}
shinyApp(ui, server)
When I click on the icon to download the graphic, the name of the graphic is "newplot":
How can I change the name "newplot(27)" by for example "ResultDNA(27)"
Thanks.
Currently there are two solutions:
By using export function in plotly
App1 <- plot_ly()
export(App1 , file = "ResultDNA(27).png")
You can 1st save the image in HTML and then in desired format using htmlwidgets
App2 <- plot_ly(...)
htmlwidgets::saveWidget(App2, file = "ResultDNA(27).html")