I just started to use Shiny and I'm having some troubles with my code. I tried several codes and searched a lot but I can't figure out how to create what I need.
I have a .R file that has several formulas and in the end returns a .csv file. I need to make this more "user friendly", so I'm trying to do that with Shiny.
This is what I've achieved so far...
So, I need a field to insert a password and if the password is correct, I can press Run Code. The Run Code must run the code with the formulas and create a csv file. Fisrtly I tried to connect the shiny app with .R file, then I tried to insert the code in Shiny and create a table that will be downloaded using the download data button.
Previous Code
library(shiny)
library(shinyWidgets)
library(shinydashboard)
CODE <- function(){
DATA <- data.frame(
Name = c("Claudia","Dan","Mike","Brian","Gary"),
Age = c(3,51,20,45,5)
return(DATA)
}
ui<- fluidPage(tags$head(tags$style(
HTML('
#sidebar {
background-color: #CCCCCC ;
}
body, label, input, button, select {
font-family: "Arial";
}
<script type="text/javascript">
$(document).ready(function() {
$("#downloadData").click(function() {
var filtered_table_data = $("#DataTables_Table_0").dataTable()._("tr", {"filter":"applied"});
Shiny.onInputChange("filtered_table", filtered_table_data);
});
});
</script>')
)),
setBackgroundColor(
color = c("#FFFFFF", "#999999"),
gradient = "linear",
direction = "bottom"
),#headerPanel(
# HTML('<p><img src="dhp_logo.png"/></p>')
#),
dashboardHeader(title = span("TEST ",style = "color: #999999; font-size: 50px;font-weight: bold")),
sidebarLayout(sidebarPanel(id="sidebar",passwordInput("password", "Password:"),
actionButton("go", "Go"),
verbatimTextOutput("DHP2021")),
mainPanel(actionButton("run", "Run Code",icon("refresh", "fa-3x"), width="400px",style="color: #FFFFFF; background-color: #666666; border-color: #666666")
,downloadButton('downloadData', 'Download Data'),dataTableOutput("table")
)))
server <- function(input, output, session){
output$value <- renderText({
req(input$go)
isolate(input$password)
})
Data <- reactive({
DATA <- input$run
return(data.frame(DATA))
})
output$table <- renderDataTable({
Data()
}, options = list(sDom = "ilftpr"))
ProcessedFilteredData <- reactive({
v <- input$filtered_table_data
col_names <- names(Data())
n_cols <- length(col_names)
n_row <- length(v)/n_cols
m <- matrix(v, ncol = n_cols, byrow = TRUE)
df <- data.frame(m)
names(df) <- col_names
return(df)
})
output$downloadData <- downloadHandler(
filename = function() { 'DATA.csv' }, content = function(file) {
write.csv(ProcessedFilteredData(), file, row.names = FALSE)
})
}
shinyApp(ui,server)
UPDATE
library(shiny)
library(shinyWidgets)
library(shinydashboard)
CODE <- function(){
DATA <- data.frame(
Name = c("Claudia","Dan","Mike","Brian","Gary"),
Age = c(3,51,20,45,5)
return(DATA)
}
Logged = FALSE
my_username <- "test"
my_password <- "test"
ui1 <- function(){
tagList(
div(id = "login",
wellPanel(textInput("userName", "Username"),
passwordInput("passwd", "Password"),
br(),
actionButton("Login", "Log in"),
verbatimTextOutput("dataInfo")
)
),
tags$style(type="text/css", "#login {font-size:30px; text-align: left;position:absolute;top: 40%;center: 90%}")
)}
ui2 <- function(){tagList(
"Successful login!"
)
mainPanel(actionButton("run", "Run Code",icon("refresh", "fa-3x"), width="400px",style="color: #FFFFFF; background-color: #666666; border-color: #666666")
,downloadButton('downloadData', 'Download Data'),dataTableOutput("table")
) }
header <- dashboardHeader(title = "Login")
sidebar <- dashboardSidebar()
body <- dashboardBody(htmlOutput("page"))
ui = dashboardPage(header, sidebar, body)
server <- function(input, output, session){
Logged <- FALSE
Security <- TRUE
USER <- reactiveValues(Logged = Logged)
SEC <- reactiveValues(Security = Security)
observe({
if (USER$Logged == FALSE) {
if (!is.null(input$Login)) {
if (input$Login > 0) {
Username <- isolate(input$userName)
Password <- isolate(input$passwd)
if(my_username == Username & my_password == Password) {
USER$Logged <- TRUE
} else {SEC$Security <- FALSE}
}
}
}
})
observe({
if (USER$Logged == FALSE) {output$page <- renderUI({ui1()})}
if (USER$Logged == TRUE) {output$page <- renderUI({ui2()})}
if(USER$Logged == TRUE){output$run <- reactiveUI(CODE)}
if(USER$Logged==TRUE){output$table <- renderDataTable(output$run())}
if(USER$Logged==TRUE){output$downloadData <- downloadHandler(
filename = function() { 'Data.csv' }, content = function(file) {
write.csv(Data(), file, row.names = FALSE)})
}})
observe({
output$dataInfo <- renderText({
if (SEC$Security) {""}
else {"Incorrect username or password"}
})
})
}
What is going wrong?
- I can't run code or create the table
- Warning: Error in $.shinyoutput: Reading from shinyoutput object is not allowed.
As I previously said, I'm just starting to use Shiny, so probably I'm making code mistakes that I can't perceive yet. I hope the example helps, if you need me to be more enlightening, just say.
Thanks in advance for any help or advice you can give me
=)